//
//  SXVideoDetailInfoView.m
//  ShiXun
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXVideoDetailInfoView.h"
#import "UIImageView+WebCache.h"
#import "UIView+UIView.h"

@interface SXVideoDetailInfoView ()

@property (nonatomic, strong) NSTimer *titleTimer; // 定时器
@property (nonatomic, strong) NSTimer *timeTimer; // 定时器
@property (nonatomic, strong) NSTimer *descriptionTimer; // 定时器

@end

@implementation SXVideoDetailInfoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _backImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:_backImageView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, kScreenWidth, 35)];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [UIFont systemFontOfSize:17];
        [self addSubview:_titleLabel];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(5, 40, kScreenWidth / 2, 1)];
        lineView.backgroundColor = [UIColor whiteColor];
        [self addSubview:lineView];
        
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 45, kScreenWidth - 10, 20)];
        _timeLabel.font = [UIFont systemFontOfSize:13];
        _timeLabel.textColor = [UIColor whiteColor];
        [self addSubview:_timeLabel];
        
        _descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 70, kScreenWidth - 10, 20)];
        _descriptionLabel.numberOfLines = 0;
        _descriptionLabel.textColor = [UIColor whiteColor];
        _descriptionLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_descriptionLabel];
        
        _collectionButtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _collectionButtn.frame = CGRectMake(kScreenWidth - 45, 10, 25, 25);
        [_collectionButtn setBackgroundImage:[UIImage imageNamed:@"SX-shoucang"] forState:(UIControlStateNormal)];
        [_collectionButtn setBackgroundImage:[UIImage imageNamed:@"SX-yuedushoucang1"] forState:(UIControlStateSelected)];
        [self addSubview:_collectionButtn];
    }
    return self;
}

- (void)setConfigureModel:(SXVideoDetailModel *)configureModel
{
    _configureModel = configureModel;
    
    [_backImageView sd_setImageWithURL:[NSURL URLWithString:configureModel.coverBlurred] placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
    
    CGFloat H = [self heightOfString:configureModel.descriptionInfo withWidht:kScreenWidth - 10 withFontSize:13];
    _descriptionLabel.height = H;
    
    self.titleTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(configureTitle) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.titleTimer forMode:NSRunLoopCommonModes];
    
    self.timeTimer = [NSTimer scheduledTimerWithTimeInterval:0.07 target:self selector:@selector(configureTime) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timeTimer forMode:NSRunLoopCommonModes];
    
    self.descriptionTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(configureDes) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.descriptionTimer forMode:NSRunLoopCommonModes];
}

// 字符串高度
- (CGFloat)heightOfString:(NSString *)str withWidht:(CGFloat)width withFontSize:(CGFloat)size
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:size] forKey:NSFontAttributeName];
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(width, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bounds.size.height;
}

- (void)configureTitle
{
    static NSInteger a = 0;
    _titleLabel.text = [_configureModel.title substringToIndex:a];
    a++;
    if (a > _configureModel.title.length) {
        [self.titleTimer invalidate];
        self.titleTimer = nil;
        a = 0;
    }
}

- (void)configureTime
{
    static NSInteger b = 0;
    NSString *timeStr = [NSString stringWithFormat:@"#%@ / %@", _configureModel.category, _configureModel.durationTime];
    _timeLabel.text = [timeStr substringToIndex:b];
    b++;
    if (b > timeStr.length) {
        [self.timeTimer invalidate];
        self.timeTimer = nil;
        b = 0;
    }
}

- (void)configureDes
{
    static NSInteger c = 0;
    _descriptionLabel.text = [_configureModel.descriptionInfo substringToIndex:c];
    c++;
    if (c > _configureModel.descriptionInfo.length) {
        [self.descriptionTimer invalidate];
        self.descriptionTimer = nil;
        c = 0;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
